//
// Created by wuzhe on 2022/3/30.
//

#ifndef UNIVERSEROOM_PLAYER_HH
#define UNIVERSEROOM_PLAYER_HH
#include <string>
#include <list>
#include "Space.hh"
#include "state/PlayerState.h"

class Room;
class PlayerState;
/**
 * 玩家
 */
class Player: public Entity {
private:
    std::string roleName;
//    int lastActive;
public:
    Space& space;
    Room* currentRoom = nullptr;
    PlayerState *playerState = nullptr;
    // 玩家IP:port地址
    std::string IP;

    Player(int num);
    virtual ~Player();
    void connect();
    void reconnect();
    void disconnect();

    /**
     * 创建房间
     * @param roomName
     * @return
     */
    void createRoom(std::string roomName);
    /**
     * 查找房间
     * @param roomName
     * @return
     */
    std::list<Room> selectRoom(std::string roomName);
    /**
     * 删除房间
     * @param roomId
     */
    void deleteRoom(int roomId);
    /**
     * 设置角色昵称
     * @param roomId
     */
    void setRoleName(std::string roleName);

    /**
     * send msg
     */
    void sendMsg(std::string msg);

    void sendNeedRoleName();

    void sendAck();

    void sendErr();
    void sendErr(const std::string errMsg);

    Entity* process(ParseRes &res) override;

    void onEnterRoom(int roomId, bool isReconnect = false);
    void onExitRoom(int roomId);

    std::string& getRoleName() {
        return roleName;
    }

    void doBroadcast(Msg msg);

    void getHistory();

    std::string toString();

    void updatePlayerInfo();

    void createRoomCB(Room *pRoom);
};


#endif //UNIVERSEROOM_PLAYER_HH
